package 双指针;

/**
 * @Author Weton Li
 * @Date 2021/3/7 16:45
 */
public class 六_搜索二维矩阵 {

    public static boolean searchMatrix(int[][] matrix, int target) {

        int m = matrix.length; // 行数
        int n = matrix[0].length; // 列数 每行的个数
        if (m == 0) return false;

        int startp = 0;
        int endp = m * n - 1;
        while (startp <= endp) {

            int mid = (startp + endp) / 2;
            int midElement = matrix[mid / n][mid % n]; // 中间值除以 每行的个数即为当前行，取余即为当前列（数组从0开始，刚刚好）

            if (target < midElement) {
                endp = mid - 1;
            } else if (target > midElement) {
                startp = mid + 1;
            } else {
                return true;
            }
        }

        return false;
    }

    public static void main(String[] args) {
//matrix = [[1,3,5,7],[10,11,16,20],[23,30,34,60]], target = 3
        int[][] input = {{1,3,5,7},{10,11,16,20},{23,30,34,60}};
        System.out.println(searchMatrix(input,60));
    }
}
